//
//  SubscibeDetailViewController.m
//  安好
//
//  Created by qinlongqing on 14/12/10.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "SubscibeDetailViewController.h"
#import "PopoverView.h"
#import "AppConfig.h"
#import "SubscibeViewController.h"
#import "SubscibeModel.h"

@interface SubscibeDetailViewController ()<UIWebViewDelegate>
{
    UIWebView *webView;
}
@end

@implementation SubscibeDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self makeNavBar];
    
    webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64)];
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.subscibeModel.urlForSubscibe]]];
    [self showActionView];
    webView.delegate = self;
    [self.view addSubview:webView];
}

-(void)makeNavBar{
    self.title = self.subscibeModel.titleForSubscibe;
    
    UIBarButtonItem *backBtnImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_返回.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backBtnclicked)];
    backBtnImg.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = backBtnImg;
    
    UIButton *setBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [setBtn setImage:[UIImage imageNamed:@"公用_更多"] forState:UIControlStateNormal];
    [setBtn addTarget:self action:@selector(setBtnclicked:) forControlEvents:UIControlEventTouchUpInside];
    [setBtn sizeToFit];
    UIBarButtonItem *setBtnItem = [[UIBarButtonItem alloc]initWithCustomView:setBtn];
    self.navigationItem.rightBarButtonItem = setBtnItem;
}

- (void)backBtnclicked{
    [self.subscibeViewController updateIsCollectionWithNid:self.subscibeModel.nidForSubscibe andIsCollection:self.subscibeModel.iscollectionForSubscibe];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setBtnclicked:(UIButton *)sender{
    CGPoint point = CGPointMake(sender.frame.origin.x + sender.frame.size.width/2, 64);
    PopoverView *popView;
    if (self.subscibeModel.iscollectionForSubscibe == 0) {
        popView = [[PopoverView alloc]initWithPoint:point titles:[NSArray arrayWithObjects:@"关注",@"分享", nil] images:[NSArray arrayWithObjects:@"subscibe_collection",@"百科_分享", nil] selectImage:[NSArray arrayWithObjects:@"subscibe_collection_select",@"百科_分享成功", nil]];
    }
    else{
        popView = [[PopoverView alloc]initWithPoint:point titles:[NSArray arrayWithObjects:@"已关注",@"分享", nil] images:[NSArray arrayWithObjects:@"subscibe_collection_select",@"百科_分享", nil] selectImage:[NSArray arrayWithObjects:@"subscibe_collection_select",@"百科_分享成功", nil]];
    }
    popView.selectRowAtIndex = ^(NSInteger index){
        if (index == 0) {
            [self showActionView];
            if (self.subscibeModel.iscollectionForSubscibe == 0) {//点击收藏
                NSDictionary *parameters = @{@"platform": kPlatform,@"version":kVersion,@"type":@"6",@"title":self.title,@"objectid":self.subscibeModel.nidForSubscibe};
                [[AFHTTPRequestOperationManager manager] POST:[NSString stringWithFormat:@"%@%@",kBaseURL,kAddCollectionURL] parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
                    NSNumber * statusNum = [responseObject objectForKey:@"status"];
                    NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
                    NSString * statusString = [numberFormatter stringFromNumber:statusNum];
                    if ([statusString isEqualToString:@"0"]) {
                        
                    }
                    else{
                        self.subscibeModel.iscollectionForSubscibe = 1;
//                        UIAlertView *pointAlert = [[UIAlertView alloc]initWithTitle:[NSString stringWithFormat:@"您已经成功关注了%@",self.title]
//                                                                            message:@"您可以在'我'-'我的关注'中查看所有关注."
//                                                                           delegate:self
//                                                                  cancelButtonTitle:@"我知道了"
//                                                                  otherButtonTitles:nil, nil];
//                        [pointAlert show];
                        [self showCustomHudSingleLine:@"已收藏"];
                    }
                    [self hiddenActionView];
                } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                    [self hiddenActionView];
                }];
            }
            else{//取消收藏
                NSDictionary *parameters = @{@"platform": kPlatform,@"version":kVersion,@"type":@"6",@"title":self.title,@"objectid":self.subscibeModel.nidForSubscibe};
                [[AFHTTPRequestOperationManager manager] POST:[NSString stringWithFormat:@"%@%@",kBaseURL,kDeleteCollectionURL] parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
                    NSNumber * statusNum = [responseObject objectForKey:@"status"];
                    NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
                    NSString * statusString = [numberFormatter stringFromNumber:statusNum];
                    if ([statusString isEqualToString:@"0"]) {

                    }
                    else{
                        self.subscibeModel.iscollectionForSubscibe = 0;
//                        UIAlertView *pointAlert = [[UIAlertView alloc]initWithTitle:[NSString stringWithFormat:@"您已经成功取消关注%@",self.title]
//                                                                            message:nil
//                                                                           delegate:self
//                                                                  cancelButtonTitle:@"我知道了"
//                                                                  otherButtonTitles:nil, nil];
//                        [pointAlert show];
                        [self showCustomHudSingleLine:@"已取消收藏"];
                    }
                    [self hiddenActionView];
                } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                    [self hiddenActionView];
                }];
            }
        }
        else{//分享
            [self doToShare];
        }
    };
    [popView show];
}

- (void)doToShare{
    //分享
    [self showShareView];
}

#pragma 实现share方法，通过点击的视图的tag值判断是什么类型的分享
- (void)share:(UITapGestureRecognizer *)shareTap{
    switch (shareTap.view.tag) {
        case 11:{//微信
            [self weixinShare:self.subscibeModel.shareurlForSubscibe :self.subscibeModel.titleForSubscibe :self.subscibeModel.contentForSubscibe :[UIImage imageWithContentsOfFile:self.localPathForImage]];
        }
            break;
        case 12:{//朋友圈
            [self weixinFriendShare:self.subscibeModel.shareurlForSubscibe :self.subscibeModel.titleForSubscibe :self.subscibeModel.contentForSubscibe :[UIImage imageWithContentsOfFile:self.localPathForImage]];
        }
            break;
        case 13:{//短信
            [self showSMSPicker:self.subscibeModel.shareurlForSubscibe :self.subscibeModel.titleForSubscibe :self.subscibeModel.contentForSubscibe :nil];
        }
            break;
        case 14:{//QQ
            [self qqShare:self.subscibeModel.shareurlForSubscibe :self.subscibeModel.titleForSubscibe :self.subscibeModel.contentForSubscibe :[UIImage imageWithContentsOfFile:self.localPathForImage]];
        }
            break;
        case 15:{//QQ空间
            [self qqZoneShare:self.subscibeModel.shareurlForSubscibe :self.subscibeModel.titleForSubscibe :self.subscibeModel.contentForSubscibe :[UIImage imageWithContentsOfFile:self.localPathForImage]];
        }
            break;
        case 16:{//新浪微博
            [self sinaWeiboShare:self.subscibeModel.shareurlForSubscibe :self.subscibeModel.titleForSubscibe :self.subscibeModel.contentForSubscibe :[UIImage imageWithContentsOfFile:self.localPathForImage]];
        }
            break;
            
        default:
            break;
    }
}


- (void)webViewDidFinishLoad:(UIWebView *)webView{
    [self hiddenActionView];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    [self hiddenActionView];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
